﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using CMISLibrary.Libraries;
using CMISLibrary;
using System.Data;
using System.Threading;
using BaocaoNPC.Usercontrols.reports;
using BaocaoNPC.common;

namespace CMIS2.BaocaoNPC.Usercontrols
{
    /// <summary>
    /// Interaction logic for INTOGCS.xaml
    /// </summary>
    public partial class INTOGCS : UserControl
    {
        #region "KhaiBaoBien"
        /*
        private string strMa_dviqly = UserInfomation.MaDViqly != "" && UserInfomation.MaDViqly != null ? UserInfomation.MaDViqly : "PA04TP";
        private string strTenDNHap = UserInfomation.TenDNHap != "" && UserInfomation.TenDNHap != null ? UserInfomation.TenDNHap : "PD0400";
        private string strCurrentLibID = MenuInfomation.LibID != "" && MenuInfomation.LibID != null ? MenuInfomation.LibID : "999";
        private string strTen_dviqly = UserInfomation.TenDViQLy != "" && UserInfomation.TenDViQLy != null ? UserInfomation.TenDViQLy : "Dien luc Thai Nguyen";
        private string strTen_dviqly_CT = UserInfomation.TenDViQLy_CT != "" && UserInfomation.TenDViQLy_CT != null ? UserInfomation.TenDViQLy_CT : "Cong ty Dien luc Thai Nguyen";
         * */

        public String strMa_dviqly = NPCUS_DanhMuc.Pglobal.strMa_dviqly != "" && NPCUS_DanhMuc.Pglobal.strMa_dviqly != null ? NPCUS_DanhMuc.Pglobal.strMa_dviqly : "PA";
        public String strTenDNHap = NPCUS_DanhMuc.Pglobal.strTen_dangnhap != "" && NPCUS_DanhMuc.Pglobal.strTen_dangnhap != null ? NPCUS_DanhMuc.Pglobal.strTen_dangnhap : "PA";
        public String strTen_dviqly = NPCUS_DanhMuc.Pglobal.strTen_dviqly != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly : "cty..";
        public String strTen_dviqly_CT = NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != "" && NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT != null ? NPCUS_DanhMuc.Pglobal.strTen_dviqly_CT : "cap tren";

        String strKy = "";
        String strLoaiToGCS = "";
        String strKieuGiay = "";
        String strMatGiay = "";

        public Int16 i16ThangHD = 0;
        public Int16 i16NamHD = 0;
        public Int16 i16Ky = 0;
        public string strMa_sogcs = "";
        public string strMa_khang = "";

        public string strMa_SoGCS_OnGrid = "";

        public string strMa_sogcs1 = " ";
        public string strNgay_ghi = " ";
        public string strSo_ky = " ";
        public string strTen_sogcs = " ";

        public string strErr = "";

        public Int16 sodongtren1trang = 25;

        public DataSet dsKetQua;

        #endregion

        #region "ThuTuc"
        //PA05: Bac Giang
        private void InToGCS_PA05(string str_MatGiay, string str_KieuGiay, DataTable dt_InToGCS)
        {
            try
            {
                CrystalDecisions.CrystalReports.Engine.LineObject Line;

                string strchuoigia = "";
                string strc1 = "";
                string strc2 = "";
                string strc3 = "";
                string strdm1 = "";
                string strdm2 = "";
                string strdm3 = "";
                string strdg1 = "";
                string strdg2 = "";
                string strdg3 = "";

                dt_InToGCS.Columns.Add("Dinhmuc1");
                dt_InToGCS.Columns.Add("Dinhmuc2");
                dt_InToGCS.Columns.Add("Dinhmuc3");
                dt_InToGCS.Columns.Add("Dongia1");
                dt_InToGCS.Columns.Add("Dongia2");
                dt_InToGCS.Columns.Add("Dongia3");
                dt_InToGCS.AcceptChanges();

                for (int ii = 0; ii < dt_InToGCS.Rows.Count; ii++)
                {
                    strchuoigia = "";
                    strc1 = "";
                    strc2 = "";
                    strc3 = "";
                    strdm1 = "";
                    strdm2 = "";
                    strdm3 = "";
                    strdg1 = "";
                    strdg2 = "";
                    strdg3 = "";

                    strchuoigia = dt_InToGCS.Rows[ii]["CHUOI_GIA"].ToString();
                    string[] mang = strchuoigia.Split('+');

                    for (int sl = 0; sl < mang.Count(); sl++)
                    {
                        if (sl == 0)
                        {
                            strc1 = mang[0];
                        }

                        if (sl == 1)
                        {
                            strc2 = mang[1];
                        }

                        if (sl == 2)
                        {
                            strc3 = mang[2];
                        }
                    }

                    if (strc1 != string.Empty)
                    {
                        string[] arr1 = strc1.Substring(4).Trim().Split('*');
                        strdm1 = arr1[0];
                        strdg1 = arr1[1];
                    }

                    if (strc2 != string.Empty)
                    {
                        string[] arr2 = strc2.Trim().Split('*');
                        strdm2 = arr2[0];
                        strdg2 = arr2[1];
                    }

                    if (strc3 != string.Empty)
                    {
                        string[] arr3 = strc3.Trim().Split('*');
                        strdm3 = arr3[0];
                        strdg3 = arr3[1];
                    }


                    dt_InToGCS.Rows[ii]["DINHMUC1"] = strdm1;
                    dt_InToGCS.Rows[ii]["DINHMUC2"] = strdm2;
                    dt_InToGCS.Rows[ii]["DINHMUC3"] = strdm3;
                    dt_InToGCS.Rows[ii]["DONGIA1"] = strdg1;
                    dt_InToGCS.Rows[ii]["DONGIA2"] = strdg2;
                    dt_InToGCS.Rows[ii]["DONGIA3"] = strdg3;
                    dt_InToGCS.AcceptChanges();

                    
                }
                    #region                    
                    

                    switch (str_MatGiay)
                    {
                        case "MT":
                            switch (str_KieuGiay)
                            {
                                case "GM":
                                    rpt_PC1_PA05_MatTruoc crt_MT_GM = new rpt_PC1_PA05_MatTruoc();
                                    crt_MT_GM.SetDataSource(dt_InToGCS);
                                    crt_MT_GM.SetParameterValue("Form", "true");
                                    crt_MT_GM.SetParameterValue("Ten_dviqly", strTen_dviqly);

                                    
                                    //crt_MT_GM.SetParameterValue("Dinhmuc1", strdm1);
                                    //crt_MT_GM.SetParameterValue("Dinhmuc2", strdm2);
                                    //crt_MT_GM.SetParameterValue("Dinhmuc3", strdm3);
                                    //crt_MT_GM.SetParameterValue("Dongia1", strdg1);
                                    //crt_MT_GM.SetParameterValue("Dongia2", strdg2);
                                    //crt_MT_GM.SetParameterValue("Dongia3", strdg3);

                                    //Section 2 -- Page header
                                    for (Int16 i = 7; i < 24; i++)
                                    {
                                        Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MT_GM.Section2.ReportObjects["Line" + i.ToString()];
                                        Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                    }
                                    //Section 3 -- Details
                                    for (Int16 i = 24; i < 53; i++)
                                    {
                                        Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MT_GM.Section2.ReportObjects["Line" + i.ToString()];
                                        Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                    }
                                    //Section 4 -- Page footer
                                    for (Int16 i = 53; i < 67; i++)
                                    {
                                        Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MT_GM.Section2.ReportObjects["Line" + i.ToString()];
                                        Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                    }

                                    UtilReport.RptManager.showReportToWin(this, crt_MT_GM, strMa_dviqly);
                                    break;
                                case "GT":
                                    rpt_PC1_PA05_MatTruoc crt_MT_GT = new rpt_PC1_PA05_MatTruoc();

                                    //crt_MT_GT.SetParameterValue("Dinhmuc1", strdm1);
                                    //crt_MT_GT.SetParameterValue("Dinhmuc2", strdm2);
                                    //crt_MT_GT.SetParameterValue("Dinhmuc3", strdm3);
                                    //crt_MT_GT.SetParameterValue("Dongia1", strdg1);
                                    //crt_MT_GT.SetParameterValue("Dongia2", strdg2);
                                    //crt_MT_GT.SetParameterValue("Dongia3", strdg3);

                                    crt_MT_GT.SetDataSource(dt_InToGCS);
                                    crt_MT_GT.SetParameterValue("Form", "false");
                                    crt_MT_GT.SetParameterValue("Ten_dviqly", strTen_dviqly);
                                    UtilReport.RptManager.showReportToWin(this, crt_MT_GT, strMa_dviqly);
                                    break;
                            }
                            break;
                        case "MS":
                            switch (str_KieuGiay)
                            {
                                case "GM":
                                    rpt_PC1_PA05_MatSau crt_MS_GM = new rpt_PC1_PA05_MatSau();
                                    crt_MS_GM.SetDataSource(dt_InToGCS);
                                    crt_MS_GM.SetParameterValue("Form", "true");
                                    crt_MS_GM.SetParameterValue("Ten_dviqly", strTen_dviqly);



                                    //Section 2 -- Page header
                                    for (Int16 i = 8; i < 24; i++)
                                    {
                                        Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MS_GM.Section2.ReportObjects["Line" + i.ToString()];
                                        Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                    }
                                    //Section 3 -- Details
                                    for (Int16 i = 24; i < 53; i++)
                                    {
                                        Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MS_GM.Section2.ReportObjects["Line" + i.ToString()];
                                        Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                    }
                                    //Section 4 -- Page footer
                                    for (Int16 i = 53; i < 67; i++)
                                    {
                                        Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MS_GM.Section2.ReportObjects["Line" + i.ToString()];
                                        Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                    }
                                    UtilReport.RptManager.showReportToWin(this, crt_MS_GM, strMa_dviqly);
                                    break;
                                case "GT":
                                    rpt_PC1_PA05_MatSau crt_MS_GT = new rpt_PC1_PA05_MatSau();
                                    crt_MS_GT.SetDataSource(dt_InToGCS);
                                    crt_MS_GT.SetParameterValue("Form", "false");
                                    crt_MS_GT.SetParameterValue("Ten_dviqly", strTen_dviqly);
                                    UtilReport.RptManager.showReportToWin(this, crt_MS_GT, strMa_dviqly);
                                    break;
                            }
                            break;
                    }

                    #endregion

                
                
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //PA12: Tuyen Quang
        private void InToGCS_PA12(string str_MatGiay, string str_KieuGiay, DataTable dt_InToGCS)
        {
            try
            {
                CrystalDecisions.CrystalReports.Engine.LineObject Line;
                switch (str_MatGiay)
                {
                    case "MT":
                        switch (str_KieuGiay)
                        {
                            case "GM":
                                rpt_PC1_PA12_MatTruoc crt_MT_GM = new rpt_PC1_PA12_MatTruoc();
                                crt_MT_GM.SetDataSource(dt_InToGCS);
                                crt_MT_GM.SetParameterValue("Form", "true");

                                //Section 2 -- Page header
                                for (Int16 i = 1; i < 72; i++)
                                {
                                    Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MT_GM.Section2.ReportObjects["Line" + i.ToString()];
                                    Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                }

                                UtilReport.RptManager.showReportToWin(this, crt_MT_GM, strMa_dviqly);
                                break;
                            case "GT":
                                rpt_PC1_PA12_MatTruoc crt_MT_GT = new rpt_PC1_PA12_MatTruoc();
                                crt_MT_GT.SetDataSource(dt_InToGCS);
                                crt_MT_GT.SetParameterValue("Form", "false");
                                UtilReport.RptManager.showReportToWin(this, crt_MT_GT, strMa_dviqly);
                                break;
                        }
                        break;
                    case "MS":
                        switch (str_KieuGiay)
                        {
                            case "GM":
                                rpt_PC1_PA12_MatSau crt_MS_GM = new rpt_PC1_PA12_MatSau();
                                crt_MS_GM.SetDataSource(dt_InToGCS);
                                crt_MS_GM.SetParameterValue("Form", "true");
                                //Section 2 -- Page header
                                for (Int16 i = 1; i < 57; i++)
                                {
                                    Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MS_GM.Section2.ReportObjects["Line" + i.ToString()];
                                    Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                }
                                
                                UtilReport.RptManager.showReportToWin(this, crt_MS_GM, strMa_dviqly);
                                break;
                            case "GT":
                                rpt_PC1_PA12_MatSau crt_MS_GT = new rpt_PC1_PA12_MatSau();
                                crt_MS_GT.SetDataSource(dt_InToGCS);
                                crt_MS_GT.SetParameterValue("Form", "false");
                                UtilReport.RptManager.showReportToWin(this, crt_MS_GT, strMa_dviqly);
                                break;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //PA13: Nghe An
        private void InToGCS_PA13(string str_MatGiay, string str_KieuGiay, DataTable dt_InToGCS)
        {
            try
            {
                CrystalDecisions.CrystalReports.Engine.LineObject Line;
                switch (str_MatGiay)
                {
                    case "MT":
                        switch (str_KieuGiay)
                        {
                            case "GM":
                                rpt_PC1_PA13_MatTruoc crt_MT_GM = new rpt_PC1_PA13_MatTruoc();
                                crt_MT_GM.SetDataSource(dt_InToGCS);
                                crt_MT_GM.SetParameterValue("Form", "true");
                                //Section 2 -- Page header
                                for (Int16 i = 31; i < 36; i++)
                                {
                                    Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MT_GM.Section2.ReportObjects["Line" + i.ToString()];
                                    Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                }
                                //Section 3 -- Details
                                for (Int16 i = 1; i < 26; i++)
                                {
                                    Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MT_GM.Section2.ReportObjects["Line" + i.ToString()];
                                    Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                }
                                UtilReport.RptManager.showReportToWin(this, crt_MT_GM, strMa_dviqly);
                                break;
                            case "GT":
                                rpt_PC1_PA13_MatTruoc crt_MT_GT = new rpt_PC1_PA13_MatTruoc();
                                crt_MT_GT.SetDataSource(dt_InToGCS);
                                crt_MT_GT.SetParameterValue("Form", "false");
                                UtilReport.RptManager.showReportToWin(this, crt_MT_GT, strMa_dviqly);
                                break;
                        }
                        break;
                    case "MS":
                        switch (str_KieuGiay)
                        {
                            case "GM":
                                rpt_PC1_PA13_MatSau crt_MS_GM = new rpt_PC1_PA13_MatSau();
                                crt_MS_GM.SetDataSource(dt_InToGCS);
                                crt_MS_GM.SetParameterValue("Form", "true");
                                //Section 2 -- Page header
                                for (Int16 i = 21; i < 25; i++)
                                {
                                    Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MS_GM.Section2.ReportObjects["Line" + i.ToString()];
                                    Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                }
                                //Section 3 -- Details
                                for (Int16 i = 1; i < 26; i++)
                                {
                                    Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MS_GM.Section2.ReportObjects["Line" + i.ToString()];
                                    Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                }
                                UtilReport.RptManager.showReportToWin(this, crt_MS_GM, strMa_dviqly);
                                break;
                            case "GT":
                                rpt_PC1_PA13_MatSau crt_MS_GT = new rpt_PC1_PA13_MatSau();
                                crt_MS_GT.SetDataSource(dt_InToGCS);
                                crt_MS_GT.SetParameterValue("Form", "false");
                                UtilReport.RptManager.showReportToWin(this, crt_MS_GT, strMa_dviqly);
                                break;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //PA15: Son La
        private void InToGCS_PA15(string str_MatGiay, string str_KieuGiay, DataTable dt_InToGCS)
        {
            try
            {
                CrystalDecisions.CrystalReports.Engine.LineObject Line;
                switch (str_MatGiay)
                {
                    case "MT":
                        switch (str_KieuGiay)
                        {
                            case "GM":
                                rpt_PC1_PA15_MatTruoc crt_MT_GM = new rpt_PC1_PA15_MatTruoc();
                                crt_MT_GM.SetDataSource(dt_InToGCS);
                                crt_MT_GM.SetParameterValue("Form", "true");
                                
                                //Section 3 -- Details
                                for (Int16 i = 1; i < 45; i++)
                                {
                                    Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MT_GM.Section3.ReportObjects["Line" + i.ToString()];
                                    Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                }
                                UtilReport.RptManager.showReportToWin(this, crt_MT_GM, strMa_dviqly);
                                break;
                            case "GT":
                                rpt_PC1_PA15_MatTruoc crt_MT_GT = new rpt_PC1_PA15_MatTruoc();
                                crt_MT_GT.SetDataSource(dt_InToGCS);
                                crt_MT_GT.SetParameterValue("Form", "false");
                                UtilReport.RptManager.showReportToWin(this, crt_MT_GT, strMa_dviqly);
                                break;
                        }
                        break;
                    case "MS":
                        switch (str_KieuGiay)
                        {
                            case "GM":
                                rpt_PC1_PA15_MatSau crt_MS_GM = new rpt_PC1_PA15_MatSau();
                                crt_MS_GM.SetDataSource(dt_InToGCS);
                                crt_MS_GM.SetParameterValue("Form", "true");
                                
                                //Section 3 -- Details
                                for (Int16 i = 1; i < 45; i++)
                                {
                                    Line = (CrystalDecisions.CrystalReports.Engine.LineObject)crt_MS_GM.Section3.ReportObjects["Line" + i.ToString()];
                                    Line.LineStyle = CrystalDecisions.Shared.LineStyle.NoLine;
                                }
                                UtilReport.RptManager.showReportToWin(this, crt_MS_GM, strMa_dviqly);
                                break;
                            case "GT":
                                rpt_PC1_PA15_MatSau crt_MS_GT = new rpt_PC1_PA15_MatSau();
                                crt_MS_GT.SetDataSource(dt_InToGCS);
                                crt_MS_GT.SetParameterValue("Form", "false");
                                UtilReport.RptManager.showReportToWin(this, crt_MS_GT, strMa_dviqly);
                                break;
                        }
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //Thiết lập giá trị cho combo số kỳ (1, 2, 3)
        private void SetValue_CboKy()
        {
            try
            {
                cboKy.Items.Add("1");
                cboKy.Items.Add("2");
                cboKy.Items.Add("3");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        //Lấy giá trị của combo số kỳ (1, 2, 3)
        private void GetValue_CboKy()
        {
            try
            {
                switch (cboKy.Text)
                {
                    case "1":
                        strKy = "1";
                        break;
                    case "2":
                        strKy = "2";
                        break;
                    case "3":
                        strKy = "3";
                        break;
                    default:
                        strKy = "";
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //Thiết lập giá trị cho combo số kỳ 1 - ở tab tìm sổ (1, 2, 3)
        private void SetValue_CboKy1()
        {
            try
            {
                cboKy1.Items.Add(" ");
                cboKy1.Items.Add("1");
                cboKy1.Items.Add("2");
                cboKy1.Items.Add("3");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        //Lấy giá trị của combo số kỳ 1 - ở tab tìm sổ (1, 2, 3)
        private void GetValue_CboKy1()
        {
            try
            {
                switch (cboKy1.Text)
                {
                    case " ":
                        strSo_ky = "-1";
                        break;
                    case "1":
                        strSo_ky = "1";
                        break;
                    case "2":
                        strSo_ky = "2";
                        break;
                    case "3":
                        strSo_ky = "3";
                        break;
                    default:
                        strSo_ky = "-1";
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //Thiết lập giá trị cho combo Loại tờ gcs (Cơ khí 1 pha - CK, Điện tử - DT)
        private void SetValue_CboLoaiToGCS()
        {
            try
            {
                cboLoaiToGCS.Items.Add("Cơ khí 1 pha");
                cboLoaiToGCS.Items.Add("Điện tử");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        //Lấy giá trị của combo Loại tờ gcs (Cơ khí 1 pha - CK, Điện tử - DT)
        private void GetValue_CboLoaiToGCS()
        {
            try
            {
                switch (cboLoaiToGCS.Text)
                {
                    case "Cơ khí 1 pha":
                        strLoaiToGCS = "CK";
                        break;
                    case "Điện tử":
                        strLoaiToGCS = "DT";
                        break;
                    default:
                        strLoaiToGCS = "";
                        break;
                }                
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //Thiết lập giá trị cho combo Kiểu giấy (Giấy trằng - GT, giấy mẫu - GM)
        private void SetValue_CboKieuGiay()
        {
            try
            {
                cboKieuGiay.Items.Add("Giấy mẫu");
                cboKieuGiay.Items.Add("Giấy trắng");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        //Lấy giá trị của combo Kiểu giấy (Giấy trằng - GT, giấy mẫu - GM)
        private void GetValue_CboKieuGiay()
        {
            try
            {
                switch (cboKieuGiay.Text)
                {
                    case "Giấy mẫu":
                        strKieuGiay = "GM";
                        break;
                    case "Giấy trắng":
                        strKieuGiay = "GT";
                        break;
                    default:
                        strKieuGiay = "";
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        
        //Thiết lập giá trị cho combo Mặt giấy (Mặt trước - MT, Mặt sau - MS)
        private void SetValue_CboMatGiay()
        {
            try
            {
                cboMatGiay.Items.Add("Mặt trước");
                cboMatGiay.Items.Add("Mặt sau");
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }
        //Lấy giá trị của combo Mặt giấy (Mặt trước - MT, Mặt sau - MS)
        private void GetValue_CboMatGiay()
        {
            try
            {
                switch (cboMatGiay.Text)
                {
                    case "Mặt trước":
                        strMatGiay = "MT";
                        break;
                    case "Mặt sau":
                        strMatGiay = "MS";
                        break;
                    default:
                        strMatGiay = "";
                        break;
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //Thiet lap thong tin thang nam hoa don trong he thong vao datime picker
        private void SET_THANGNAM_HD_ONDTPTHANGHD()
        {
            try
            {
                if (i16ThangHD.ToString().Length == 1)
                {
                    dtpThangNamHD.Text = "0" + i16ThangHD.ToString() + "/" + i16NamHD.ToString();
                }
                else
                {
                    dtpThangNamHD.Text = i16ThangHD.ToString() + "/" + i16NamHD.ToString();
                }
            }
            catch
            {
                dtpThangNamHD.Text = "01/" + i16NamHD.ToString();
            }
        }

        //Lay thang nam hoa don trong he thong
        private void GET_THANGNAM_HD()
        {
            try
            {
                DataTable dtThangNamHD = Utility.getThangNamCurrent("HD");
                if (dtThangNamHD != null && dtThangNamHD.Rows.Count > 0)
                {
                    i16ThangHD = Convert.ToInt16(dtThangNamHD.Rows[0]["THANG"]);
                    i16NamHD = Convert.ToInt16(dtThangNamHD.Rows[0]["NAM"]);
                }
            }
            catch
            {
                i16ThangHD = Convert.ToInt16(DateTime.Now.Month);
                i16NamHD = Convert.ToInt16(DateTime.Now.Year);
            }
        }

        //Lay cac thong tin tren form
        private void GET_VALUE_ON_FORM()
        {
            try
            {
                i16ThangHD = Convert.ToInt16(dtpThangNamHD.Text.Remove(2, 5));
                i16NamHD = Convert.ToInt16(dtpThangNamHD.Text.Substring(3, 4));
                i16Ky = Convert.ToInt16(cboKy.SelectedItem);
                strMa_sogcs = txtMa_SoGCS.Text.Trim();
                strMa_khang = txtMa_KHang.Text.Trim();
                
                GetValue_CboLoaiToGCS();
                GetValue_CboKieuGiay();
                GetValue_CboMatGiay();
                GetValue_CboKy1();

                if (txtMa_SoGCS1.Text.Trim() == "")
                {
                    strMa_sogcs1 = " ";
                }
                else
                {
                    strMa_sogcs1 = txtMa_SoGCS1.Text;
                }
                if (txtNgayGCS.Text.Trim() == "")
                {
                    strNgay_ghi = " ";
                }
                else
                {
                    strNgay_ghi = txtNgayGCS.Text;
                }
                
                if (txtTen_SoGCS.Text.Trim() == "")
                {
                    strTen_sogcs = " ";
                }
                else
                {
                    strTen_sogcs = txtTen_SoGCS.Text;
                }
                
            }
            catch
            {
                i16ThangHD = Convert.ToInt16(DateTime.Now.Month);
                i16NamHD = Convert.ToInt16(DateTime.Now.Year);
                i16Ky = 1;
                strMa_sogcs = "";
                strMa_khang = "";

                strMa_sogcs1 = " ";
                strNgay_ghi = " ";
                strSo_ky = "-1";
                strTen_sogcs = " ";
            }
        }

        //Kiểm tra thông tin các ô trên form khi nút ghi được click
        private bool CheckDataWhenClickButtonGhi(String strCase)
        {
            if (txtMa_SoGCS.Text.ToString() == "")
            {
                if (txtMa_KHang.Text.ToString() == "")
                {
                    System.Windows.MessageBox.Show("Bạn phải nhập ít nhất 1 thông tin (Mã sổ ghi chỉ số, mã khách hàng).");
                    txtMa_SoGCS.Focus();
                    return false;
                }
            }
            
            return true;
        }

        //Check chon toan bo gia tri
        private void CheckAllValue()
        {
            try
            {
                for (Int16 i = 0; i < (dsKetQua.Tables[0].Rows.Count); i++)
                {
                    dsKetQua.Tables[0].Rows[i]["CHON"] = "true"; 
                }
                DataView dv = dsKetQua.Tables[0].DefaultView;
                dgrDSach_KHang.ItemsSource = dv;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        //Check dao chon gia tri
        private void UnCheckValue()
        {
            try
            {
                for (Int16 i = 0; i < (dsKetQua.Tables[0].Rows.Count); i++)
                {
                    if (dsKetQua.Tables[0].Rows[i]["CHON"].ToString() == "True")
                    {
                        dsKetQua.Tables[0].Rows[i]["CHON"] = "False";
                    }
                    else
                    {
                        dsKetQua.Tables[0].Rows[i]["CHON"] = "True";
                    }
                }
                DataView dv = dsKetQua.Tables[0].DefaultView;
                dgrDSach_KHang.ItemsSource = dv;
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message.ToString());
            }
        }

        private void setCurrentData()
        {
            DataRowView drv = dgrDSach_SGCS.SelectedItem as DataRowView;
            if (drv != null)
            {
                strMa_SoGCS_OnGrid = drv.Row["MA_SOGCS"].ToString();
            }
            else
            {
                strMa_SoGCS_OnGrid = "";
            }
        }
        #endregion

        #region "ThuTucXuLyGiaoDien"
        //Enable va Disable cac button
        private void Enable_Disable_Button(string strCase, string strButtonPreviewClick)
        {
            try
            {
                switch (strCase)
                {
                    case "LOADFORM":
                        btnInToGCS.IsEnabled = false;
                        btnInTrangBia.IsEnabled = false;
                        btnChon.IsEnabled = false;
                        
                        txtSoDongTren1Trang.IsEnabled = false;
                        break;
                    case "TimThayDuLieuOTab1":
                        btnInToGCS.IsEnabled = true;
                        btnInTrangBia.IsEnabled = true;
                        txtSoDongTren1Trang.IsEnabled = true;
                        break;
                    case "TimThayDuLieuOTab2":
                        btnChon.IsEnabled = true;
                        btnInTrangBia.IsEnabled = true;
                        txtSoDongTren1Trang.IsEnabled = true;
                        break;
                    case "KhongTimThayDuLieuOTab1":
                        btnInToGCS.IsEnabled = false;
                        btnInTrangBia.IsEnabled = false;
                        txtSoDongTren1Trang.IsEnabled = false;
                        break;
                    case "KhongTimThayDuLieuOTab2":
                        btnInToGCS.IsEnabled = false;
                        btnInTrangBia.IsEnabled = false;
                        btnChon.IsEnabled = false;
                        txtSoDongTren1Trang.IsEnabled = false;
                        break;
                }
            }
            catch
            {
            }
        }
        #endregion

        public INTOGCS()
        {
            InitializeComponent();
            Component.TabIndexManager.RegisterTabManager(this);
            Enable_Disable_Button("LOADFORM", "");

            SetValue_CboKy();
            cboKy.SelectedIndex = 0;
            SetValue_CboKy1();
            cboKy1.SelectedIndex = 0;

            SetValue_CboLoaiToGCS();
            cboLoaiToGCS.SelectedIndex =0;
            SetValue_CboKieuGiay();
            cboKieuGiay.SelectedIndex = 0;
            SetValue_CboMatGiay();
            cboMatGiay.SelectedIndex = 0;

            GET_THANGNAM_HD();
            SET_THANGNAM_HD_ONDTPTHANGHD();
            dtpThangNamHD.Focus();

            //txtSoDongTren1Trang.Text = sodongtren1trang.ToString();

            //strMa_dviqly = "PA0504";
            //txtMa_SoGCS.Text = "04103";
            //strMa_dviqly = "PA13VH";
            //txtMa_SoGCS.Text = "VH001";
            //strMa_dviqly = "PA12TX";
            //txtMa_SoGCS.Text = "TX001";
            //strMa_dviqly = "PA15TX";
            //txtMa_SoGCS.Text = "TXXKEO1";
            //strMa_sogcs = txtMa_SoGCS.Text;
        }

        private void cmdLayDuLieu_Click(object sender, RoutedEventArgs e)
        {
            //--------------------- Kiểm tra thông tin trên form đã nhập đủ chưa??? ---------------------            
            while (CheckDataWhenClickButtonGhi("") == false)
            {
                return;
            }
            //-------------------- Điều khiển con chuột và di chuyển giữa các textbox   -----------------
            this.Cursor = Cursors.Wait;            
            Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ThreadStart(delegate { }));
            try
            {
                this.Cursor = Cursors.Wait;
                GET_VALUE_ON_FORM();
                //a. Kiem tra tinh dung dan cua du lieu
                //1. KTra So GCS co du lieu o ky-thang/nam do ko?
                //2. KTra Ma_KHang co ton tai trong csdl ko?
                //3.                
                bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
                //dsKetQua = bus.TH_INTOGCS(i16NamHD, i16ThangHD, i16Ky, strMa_dviqly, strMa_sogcs, strMa_khang);

                dsKetQua = GlobalServices.Service_NPCUS.togcs(i16NamHD, i16ThangHD, i16Ky, strMa_dviqly, strMa_sogcs, strMa_khang, ref strErr);                
                
                if (dsKetQua != null && dsKetQua.Tables.Count > 0)
                {
                    DataView dv = dsKetQua.Tables[0].DefaultView;
                    dgrDSach_KHang.ItemsSource = dv;
                    btnInToGCS.IsEnabled = true;
                    btnInTrangBia.IsEnabled = true;
                    txtSoDongTren1Trang.IsEnabled = true;
                    this.Cursor = null;
                    lblResult.Content = dsKetQua.Tables[0].Rows.Count.ToString();
                }
                else
                {
                    dgrDSach_KHang.ItemsSource = null;
                    btnInToGCS.IsEnabled = false;
                    btnInTrangBia.IsEnabled = false;
                    txtSoDongTren1Trang.IsEnabled = false;
                    this.Cursor = null;
                    MessageBox.Show(strErr.ToString());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            this.Cursor = null;
        }

        private void txtNgayGCS_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            short val;
            if (!Int16.TryParse(e.Text, out val))
            {
                e.Handled = true;
                return;
            }
            e.Handled = false;
            return;
        }        

        private void dgrDSach_KHang_CurrentCellChanged(object sender, EventArgs e)
        {

        }

        private void dtpThangNamHD_KeyDown(object sender, KeyEventArgs e)
        {
            if (dtpThangNamHD.Text.ToString() == "") 
            {
                SET_THANGNAM_HD_ONDTPTHANGHD();
            }

        }

        private void btnInToGCS_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                
                this.Cursor = Cursors.Wait;
                Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ThreadStart(delegate { }));

                //Lay du lieu cac bien
                GET_VALUE_ON_FORM();

                if (dsKetQua != null)
                {
                    DataTable dt1 = dsKetQua.Tables[0] as DataTable;
                    //DataView dv = dgrDSach_KHang.ItemsSource as DataView;
                    //DataRow[] drselect1 = dv.Table.Select("CHON is not null");

                    //DataTable dt = new DataTable();
                    //dt = dsKetQua.Tables[0].Clone();
                    //foreach (DataRow drsel in drselect1)
                    //{
                    //    if (drsel["CHON"].ToString() == "1")
                    //    {
                    //        dt.ImportRow(drsel);

                    //    }
                    //}


                    DataTable dt = dt1.Select("CHON = 'true'").CopyToDataTable();
                    dt.TableName = "InToGCS";
                    //CrystalDecisions.CrystalReports.Engine.LineObject Linea = new CrystalDecisions.CrystalReports.Engine.LineObject();
                    
                    switch (strMa_dviqly.Substring(0,4))
                    {     
                        //Bac Giang
                        case "PA05":
                            InToGCS_PA05(strMatGiay, strKieuGiay, dt);
                            break;
                        //Lang Son
                        case "PA11":
                            break;
                        //Tuyen Quang
                        case "PA12":
                            if (strMa_dviqly == "PA12TX")
                            {
                                InToGCS_PA12(strMatGiay, strKieuGiay, dt);
                            }
                            break;
                        //Nghe An
                        case "PA13":
                            InToGCS_PA13(strMatGiay, strKieuGiay, dt);
                            break;
                        //Son La
                        case "PA15":
                            InToGCS_PA15(strMatGiay, strKieuGiay, dt);
                            break;
                    }
                }
                this.Cursor = null;
            }
            catch (Exception ex)
            {
                this.Cursor = null;
                MessageBox.Show(ex.ToString());                
            }
        }

        private void chkChonTatCa_Click(object sender, RoutedEventArgs e)
        {
            CheckAllValue();
        }

        private void chkDaoChon_Click(object sender, RoutedEventArgs e)
        {
            UnCheckValue();
        }

        private void btnInTrangBia_Click(object sender, RoutedEventArgs e)
        {
            //hien thi con tro o trang thai ban
            this.Cursor = Cursors.Wait;
            Application.Current.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new ThreadStart(delegate { }));
            
            DataTable dt1 = dsKetQua.Tables[0] as DataTable;
            
            DataSet dsInTrangBia_Entity = new BCNPCEntities.InToGCS.InTrangBia();
            //DataTable dtTrangBia = dsInTrangBia_Entity.Tables["InTrangBia"].Clone();
            DataTable dtTrangBia = dsKetQua.Tables[0].Clone();
            dt1.TableName = "InMucLuc";

            //Code in muc luc theo dang STT1--TEN_KHANG1--DIACHI1  ### STT2--TEN_KHANG2--DIACHI2
            //sodongtren1trang = Convert.ToInt16(txtSoDongTren1Trang.Text);
            //Double TongSoLanLap = (dt1.Rows.Count - 1) / sodongtren1trang;
            //Int16 iDongThu = 0;
            //Int16 iLapDuoc = 0;
    
            //for (Int16 iLanLapThu = 1; iLanLapThu <= TongSoLanLap; iLanLapThu++)
            //{
            //    if (iLanLapThu % 2 == 0)
            //    {
            //        for (Int32 i = (iDongThu); i < iLanLapThu * sodongtren1trang; i++)
            //        {
            //            dsInTrangBia_Entity.Tables["InTrangBia"].Rows[i - (iLapDuoc * sodongtren1trang)]["STT2"] = dt1.Rows[i]["STT"];
            //            dsInTrangBia_Entity.Tables["InTrangBia"].Rows[i - (iLapDuoc * sodongtren1trang)]["TEN_KHANG2"] = dt1.Rows[i]["TEN_KHANG"];
            //            dsInTrangBia_Entity.Tables["InTrangBia"].Rows[i - (iLapDuoc * sodongtren1trang)]["DIA_CHI2"] = dt1.Rows[i]["DIA_CHI"];
            //            dsInTrangBia_Entity.AcceptChanges();
            //            iDongThu += 1;
            //        }
            //        //iLapDuoc += 1;
            //        iLapDuoc = iLapDuoc;
            //    }
            //    else
            //    {
            //        for (Int32 i = iDongThu; i < iLanLapThu * sodongtren1trang; i++)
            //        {
            //            DataRow dr = dsInTrangBia_Entity.Tables["InTrangBia"].NewRow();
            //            dr["STT1"] = dt1.Rows[i]["STT"];
            //            dr["TEN_KHANG1"] = dt1.Rows[i]["TEN_KHANG"];
            //            dr["DIA_CHI1"] = dt1.Rows[i]["DIA_CHI"];
            //            dsInTrangBia_Entity.Tables["InTrangBia"].Rows.Add(dr);
            //            dsInTrangBia_Entity.AcceptChanges();
            //            iDongThu += 1;
            //        }
            //        iLapDuoc += 1;
            //    }                
            //}

            //if (TongSoLanLap > 0) // xu ly truong hop so ban ghi > so dong tren 1 trang
            //{
            //    if (dt1.Rows.Count - (TongSoLanLap * sodongtren1trang) > 0)
            //    {
            //        if (TongSoLanLap % 2 != 0)
            //        {
            //            for (Int32 i = iDongThu; i <= dt1.Rows.Count - 1; i++)
            //            {
            //                dsInTrangBia_Entity.Tables["InTrangBia"].Rows[i - (iLapDuoc * sodongtren1trang)]["STT2"] = dt1.Rows[i]["STT"];
            //                dsInTrangBia_Entity.Tables["InTrangBia"].Rows[i - (iLapDuoc * sodongtren1trang)]["TEN_KHANG2"] = dt1.Rows[i]["TEN_KHANG"];
            //                dsInTrangBia_Entity.Tables["InTrangBia"].Rows[i - (iLapDuoc * sodongtren1trang)]["DIA_CHI2"] = dt1.Rows[i]["DIA_CHI"];
            //                dsInTrangBia_Entity.AcceptChanges();
            //                iDongThu += 1;
            //            }
            //        }
            //        else
            //        {
            //            //for (Int32 i = iLapDuoc * 2 * sodongtren1trang; i < dt1.Rows.Count - 1; i++)
            //            for (Int32 i = iDongThu; i <= dt1.Rows.Count - 1; i++)
            //            {
            //                DataRow dr = dsInTrangBia_Entity.Tables["InTrangBia"].NewRow();
            //                dr["STT1"] = dt1.Rows[i]["STT"];
            //                dr["TEN_KHANG1"] = dt1.Rows[i]["TEN_KHANG"];
            //                dr["DIA_CHI1"] = dt1.Rows[i]["DIA_CHI"];
            //                dsInTrangBia_Entity.Tables["InTrangBia"].Rows.Add(dr);
            //                dsInTrangBia_Entity.AcceptChanges();
            //                iDongThu += 1;
            //            }
            //        }
            //    }
            //}
            //else// xu ly truong hop so ban ghi < so dong tren 1 trang
            //{
            //    for (Int32 i = iDongThu; i <= dt1.Rows.Count - 1; i++)
            //    {
            //        DataRow dr = dsInTrangBia_Entity.Tables["InTrangBia"].NewRow();
            //        dr["STT1"] = dt1.Rows[i]["STT"];
            //        dr["TEN_KHANG1"] = dt1.Rows[i]["TEN_KHANG"];
            //        dr["DIA_CHI1"] = dt1.Rows[i]["DIA_CHI"];
            //        dsInTrangBia_Entity.Tables["InTrangBia"].Rows.Add(dr);
            //        dsInTrangBia_Entity.AcceptChanges();
            //        iDongThu += 1;
            //    }
            //}

            //hien thi ra report
            //PC1_PA05_InTrangBia crt_InTrangBia = new PC1_PA05_InTrangBia();
            //crt_InTrangBia.SetDataSource(dsInTrangBia_Entity);
            //crt_InTrangBia.SetParameterValue("Ma_quyen", strMa_sogcs);
            //UtilReport.RptManager.showReportToWin(this, crt_InTrangBia, strMa_dviqly);

            rpt_PC1_PA05_InMucLuc crt_InMucLuc = new rpt_PC1_PA05_InMucLuc();
            crt_InMucLuc.SetDataSource(dt1);
            crt_InMucLuc.SetParameterValue("Ma_quyen", strMa_sogcs);
            UtilReport.RptManager.showReportToWin(this, crt_InMucLuc, strMa_dviqly);
            //hien thi con tro o trang thai binh thuong
            this.Cursor = null;
        }

        private void btnLayDuLieu1_Click(object sender, RoutedEventArgs e)
        {
            GET_VALUE_ON_FORM();
            
            DataSet dsSoGCS = new DataSet();

            //bus_TienichNPC.cls_TienichNPC_bus bus = new bus_TienichNPC.cls_TienichNPC_bus();
            //dsSoGCS = bus.select_SoGCS("PA13VH", strMa_sogcs1, strTen_sogcs, strNgay_ghi, Convert.ToInt16(strSo_ky));

            dsSoGCS = GlobalServices.Service_NPCUS.select_SoGCS(strMa_dviqly, strMa_sogcs1, strTen_sogcs, strNgay_ghi, Convert.ToInt16(strSo_ky));

            if (dsSoGCS != null)
            {
                DataView dv = dsSoGCS.Tables[0].DefaultView;
                dgrDSach_SGCS.ItemsSource = dv;

                btnChon.IsEnabled = true;
            }
            else
            {
                dgrDSach_SGCS.ItemsSource = null;
                btnChon.IsEnabled = false;
            }
            
        }

        private void dgrDSach_SGCS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            setCurrentData();
        }

        private void btnChon_Click(object sender, RoutedEventArgs e)
        {
            tabInToGCS.Focus();
            txtMa_SoGCS.Text = strMa_SoGCS_OnGrid;
        }

        private void txtSoDongTren1Trang_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            short val;
            if (!Int16.TryParse(e.Text, out val))
            {
                e.Handled = true;
                return;
            }
            e.Handled = false;
            return;
        }
    }
}
